@extends('wap.layout')
@section('search', 'onkeydown=search();')
@section('content')
		<!--搜索框-->
<div class="weui-search-bar" id="searchBar">
	<form class="weui-search-bar__form" onkeydown="if(event.keyCode==13){return false;}">
		<div class="weui-search-bar__box">
			<i class="weui-icon-search"></i>
			<input type="search" class="weui-search-bar__input" id="searchInput" placeholder="搜索" required="">
			<a href="javascript:" class="weui-icon-clear" id="searchClear"></a>
		</div>
		<label class="weui-search-bar__label" id="searchText">
			<i class="weui-icon-search"></i>
			<span>搜索</span>
		</label>
	</form>
	<a href="javascript:" class="weui-search-bar__cancel-btn" id="searchCancel">取消</a>
</div>
<!--搜索框end-->
<!--返回-->
<div class="gobackBar">
	<a href="/index">返回上一级</a>
	<a class="orderby" id='orderby' href="javascript:void(0);">排序</a>
</div>
<!--返回end-->
<!--文件列表-->
<div class="fileList">
</div>
<!--文件列表end-->

<!--如无文件，则显示-->

<!--如无文件，则显示end-->
<!--如有文件，则显示-->
{{--<div class="pInfoBar">298389个文件夹，49857643个文件</div>--}}
		<!--如有文件，则显示end-->
<div class="placeHolderBar"></div>
<a href="javascript:;" id='show-custom' class="weui-btn weui-btn_primary" style="display:none;">显示自定义对话框</a>
<style>
	.weui-dialog__bd {font-size:18px}
</style>
<div class="mask"></div>
<div class="weui-actionsheet  weui-actionsheet_toggle orderOpt" id="oderOpt">
	<div class="weui-actionsheet__title">排序方式</div>
	<div class="weui-actionsheet__menu">
		<div class="weui-actionsheet__cell color-primary" id="order_file_name" data-key="1">按名称<span>降序</span></div>
		<div class="weui-actionsheet__cell" id="order_file_size" data-key="2">按大小<span>降序</span></div>
		<div class="weui-actionsheet__cell" id="order_file_time" data-key="3">按时间<span>降序</span></div>
	</div>
	<div class="weui-actionsheet__action">
		<div class="weui-actionsheet__cell weui-actionsheet_cancel odercancel">取消</div>
	</div>
</div>
<input type="hidden" name="key" value="1">
<input type="hidden" name="order" value="0">

@stop

@section('ownJs')
	<script src="/js/sparkmd5.min.js"></script>
	<script>
		var file_type = getParameterByName('file_type');
		var upload_time = 0,search_name,download_links;
		$(function() {
			getFile();
			var mask = $(".mask");
			orderOpt = $(".orderOpt");
			odercancel= $(".odercancel");
			$("#orderby").click(function(){
				mask.show();
				orderOpt.slideToggle('fast')
			});
			mask.click(function(){
				$(this).hide();
				orderOpt.slideToggle('fast')
			});
			odercancel.click(function(){
				mask.hide();
				orderOpt.slideToggle('fast')
			});
			$('#order_file_name, #order_file_size, #order_file_time').click(function(){
				var key = $('input[name="key"]').val();
				if(key != $(this).data('key')){
					$('input[name="key"]').val($(this).data('key'));
					$('input[name="order"]').val(0);
					$(this).addClass('color-primary');
					$(this).siblings().removeClass('color-primary').find('span').text('降序')
				}else{
					if($('input[name="order"]').val() == '0'){
						$('input[name="order"]').val(1);
						$(this).find('span').text('升序');
					}else{
						$('input[name="order"]').val(0);
						$(this).find('span').text('降序');
					}
				}
				getFile();
				mask.click();
			});
			//文件操作
			$(".fileList").on("click","i.ops",function() {
				$(this).parent("li.listItem").find("i.ops").toggleClass("open");
				$(this).parent("li.listItem").siblings().find(".ops").removeClass("open");
				$(this).parent("li.listItem").siblings().find(".opList").addClass("dis");
				$(this).parent("li.listItem").find(".opList").toggleClass("dis");
			});
			//文件菜单操作
			$(document).on("click", ".iFav", function() {
				var id = $(this).data('id');
				if($(this).hasClass("ck")){
					cancelCollectFile(id);
				}else{
					collectFile(id);
				}
			});

			//删除弹框
			$(document).on("click", ".iDel", function() {
				var id = $(this).data('id');
				$.confirm("您确定要删除文件吗?", "确认删除?", function() {
					deleteFile(id);
				}, function() {
					//取消操作
				});
			});
			//下载文件
			$(document).on("click", ".iDownload", function() {
				var id = $(this).data('id');
				var is_public = $(this).data('is_public');
				if(file_type == 'at'){
					var access_token =$(this).data('access_token');
					var is_image = $('#iDownLoad'+id).data('is_image');
					if(is_image == '0'){
						var u = navigator.userAgent, app = navigator.appVersion;
						var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
						console.log(u);
						if(isIOS){
							download_links = "{{ Illuminate\Support\Facades\Request::server ('SERVER_NAME') }}" +'/download/' + access_token;
							$('#show-custom').trigger('click');
						}else{
							location.href = '/download/' + access_token;
						}
					}else{
						location.href = '/download/' + access_token;
					}
				}else {
					download(id, is_public);
				}
			});
			$(document).on("click", "#show-custom", function() {
				$.modal({
					title: "",
					text: download_links,
					buttons: [
						{ text: "长按复制该链接到浏览器中下载", className: "default"},
//				{ text: "确定", onClick: function(){ $.alert("确定"); } },
					]
				});
			});
		});
		function search(){
			if (event.keyCode==13){  //回车键的键值为13
				search_name = $('#searchInput').val();
				getFile();
			}
		}
		/*//收藏文件
		function collectFile(id){
			$.ajax({
				type: 'get',
				url: ' /collect_file/collect/'+id,
				dataType: 'json',
				success: function (result) {
					if (result.code == 0) {
						$('#iFav'+id).toggleClass("ck");
					}else{
//					$.toast("文件收藏失败!");
					}
				}
			});
		}*/
		//取消收藏文件
		function cancelCollectFile(id){
			$.ajax({
				type: 'get',
				url: ' /collect_file/cancel/'+id,
				dataType: 'json',
				success: function (result) {
					if (result.code == 0) {
						$('#iFav'+id).toggleClass("ck");
						location.href = location.href;
					}else if(result.code == -1){
						location.href = '/error';
					}
				}
			});
		}
		//删除文件或文件夹
		function deleteFile(id){
			$.ajax({
				type: 'get',
				url: ' /user_file/delete/'+id,
				dataType: 'json',
				success: function (result) {
					if (result.code == 0) {
						$.toast("文件已经删除!");
						setTimeout(function() {
							location.href = location.href;
						}, 1000);
					}else if(result.code == -1){
						location.href = '/error';
					}
				}
			});
		}
		//下载文件
		function download(id, is_public){
			$.ajax({
				type: 'get',
				url: '/download/auth/'+id,
				data:{is_public: is_public},
				dataType: 'json',
				success: function (result) {
					console.log(result.data.access_token);
					if (result.code == 0) {
						var is_image = $('#iDownLoad'+id).data('is_image');
						if(is_image == '0'){
							var u = navigator.userAgent, app = navigator.appVersion;
							var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
							console.log(u);
							if(isIOS){
								download_links = "{{ Illuminate\Support\Facades\Request::server ('SERVER_NAME') }}" +'/download/' + result.data.access_token;
								$('#show-custom').trigger('click');
							}else{
								location.href = '/download/' + result.data.access_token;
							}
						}else{
							location.href = '/download/' + result.data.access_token;
						}
					}else if(result.code == 1){
						$.toast("文件不存在", "cancel");
						setTimeout(function() {
							location.href = location.href;
						}, 1000);
					}else if(result.code == -1){
						location.href = '/error';
					}
				}
			});
		}
		function getFile(){
			var order = $('input[name="order"]').val();
			var key = $('input[name="key"]').val();
			var url = '/collect_file/files';
			if(file_type == 'at') url = '/user_file/links';
			$.ajax({
				type:'get',
				url:url,
				data:{name:search_name,order:order,key:key},
				dataType:'json',
				success:function(result){
					if(result.code == 0){
						var files = result.data.files;
						console.log(files);//return false;
						if(files.length > 0){
							var html = '<div class="weui-panel weui-panel_access"><ul class="weui-panel__bd">';
							if(files.length > 0){
								$.each(files, function(k, v){
									var file_icon_html = '';
									var is_image = 0;
									if(v.thumb){
										file_icon_html = '<img src="'+ v.thumb +'" alt="" class="weui-media-box__thumb">';
										is_image = 1;
									}else{
										var icon = '';
										icon = getIconClass(v.file_ext.toUpperCase());
										file_icon_html = '<i class="fileIcon '+icon+'"></i>';
									}
									html += '<li  class="listItem">'
											+'<i class="ops"><b></b></i>'
											+'<a href="/detail?user_file_id='+ v.id+'&is_public='+ v.is_public+'" class="weui-media-box weui-media-box_appmsg filetype">'
											+'<div class="weui-media-box__hd">'
											+ file_icon_html
											+'</div>'
											+'<div class="weui-media-box__bd">'
											+'<h4 class="weui-media-box__title">'+ v.name+'</h4>'
											+'<p class="weui-media-box__desc">'+v.updated_at+'</p>'
											+'</div>'
											+'</a>'
											+'<div class="opList dis">'
											+'<a href="javascript:void(0);" class="iDownload" id="iDownLoad'+ v.id+'" data-id="'+ v.id+'" data-access_token="'+ v.access_token+'" data-is_image="'+is_image+'" data-is_public="'+ v.is_public+'">'
											+'<div class="btnIcon"><i></i></div>'
											+'<p class="btnLabel">下载</p>'
											+'</a>'
											+(file_type == 'at' ? '' :
											'<a href="javascript:;" class="iFav ck" id="iFav'+ v.id+'" data-id="'+ v.id+'">'
											+'<div class="btnIcon"><i></i></div>'
											+'<p class="btnLabel">收藏</p>'
											+'</a>')
											+((file_type == 'fav' && parseInt(v.is_public) == 1 ) ? '' :
											'<a href="javascript:;" class="iDel" id="iDel'+ v.id+'" data-id="'+ v.id+'">'
											+'<div class="btnIcon"><i></i></div>'
											+'<p class="btnLabel">删除</p>'
											+'</a>')
//											+'<a href="/detail?user_file_id='+ v.id+'&is_public='+ v.is_public+'" class="iMore" id="iMore'+ v.id+'" data-id="'+ v.id+'">'
//											+'<div class="btnIcon"><i></i></div>'
//											+'<p class="btnLabel">更多</p>'
											+'</a>'
											+'</div>'
											+'</li>';
								});
							}
							html += '</ul></div>';
							$('.fileList').html(html);
						}else{
							var infoHtml = '<div class="pldIcon"><i></i></div>'
									+'<div class="pInfoBar">暂无文件</div>';
							$('.fileList').html(infoHtml);
						}
					}else if(result.code == -1){
						location.href = '/error';
					}
				},
				complete:function () {

				}

			});
		}
		function getIconClass(file_ext){
			switch (file_ext){
				case '.DOC':
				case '.DOCX':
					icon = 'doc';
					break;
				case '.TXT':
					icon = 'txt';
					break;
				case '.ZIP':
				case '.RAR':
					icon = 'zip';
					break;
				case '.PPT':
					icon = 'ppt';
					break;
				case '.XLS':
				case '.XLSX':
					icon = 'excel';
					break;
				default :
					icon = 'common';
			}
			return icon;
		}
	</script>
@stop
